<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>
<body>
  <div id="root">
    姓: <input type="text" v-model="firstName"><br>
    名: <input type="text" v-model="lastName"><br>

    <p>fullName(插值): {{firstName + '-' + lastName}}</p>
    <p>fullName2(method): {{fullName2()}}</p>
    <p>fullName3(computed): {{fullName3}}</p>
    <p>fullName4(watch): {{fullName4}}</p>
  </div>

  <script src="../js/vue.js"></script>
  <script>
     const vm = new Vue({
      el: '#root',
      data: {
        firstName: 'A',
        lastName: 'B',
        fullName4: 'A-B'
      },

      methods: {
        fullName2 () {
          return this.firstName + '-' + this.lastName
        }
      },

      // 计算属性
      computed: {
        // 计算属性方法什么时候调用: 初始显示/依赖数据发生变化
        fullName3 () {
          console.log('computed fullName3')
          return this.firstName + '-' + this.lastName
        }
      },
      // 方式1: watch配置
      watch: {
        // 监视firstName
        // firstName (newVal, oldVal) {
        //   console.log('watch firstName', newVal, oldVal)
        //   // 更新fullName4
        //   this.fullName4 = newVal + '-' + this.lastName
        // },

        firstName: {
          hanlder (newVal, oldVal) {
            console.log('watch2 firstName', newVal, oldVal)
            // 更新fullName4
            this.fullName4 = newVal + '-' + this.lastName
          }
        }
      }
    })

    // 监视lastName
    vm.$watch('lastName', function (value) {
      // 更新fullName4
      this.fullName4 = this.firstName + '-' + value
    })
  </script>
</body>
</html>